﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Cache.CacheObject;

namespace Hi_Baidu.Cache
{
    /// <summary>
    ///     提供了好友缓存集合的相关操作
    /// </summary>
    public class FriendControlCacheManager : CacheManager
    {
        /// <summary>
        ///     提供了好友缓存集合的相关操作
        /// </summary>
        /// <param name="CleanCacheTimer">清除缓存超时器</param>
        /// <param name="Interval">触发间隔</param>
        public FriendControlCacheManager(System.Timers.Timer CleanCacheTimer, int Interval) : base(CleanCacheTimer,Interval)
        {
        }
        /// <summary>
        ///     向缓存集合中打入分组缓存对象
        ///     [会自动检测是否存在]
        /// </summary>
        /// <param name="itemCache">分组缓存对象</param>
        /// <returns>返回true , 则表示添加成功</returns>
        public bool AddFriendCache(IMetaData friendCache)
        {
            //如果不存在则加入缓存队列
            if (!this.Equals(friendCache))
            {
                if (!this.Add(friendCache))
                {
                    return false;
                }
            }
            //如果存在则更改
            else
            {
                DeleteFriendCache FriendCache = this.GetMemberByKey(friendCache.Key) as DeleteFriendCache;
                if (FriendCache != null)
                {
                    FriendCache.Value = (friendCache as DeleteFriendCache).Value;
                    FriendCache.ExpridTime = (friendCache as DeleteFriendCache).ExpridTime;
                }
            }
            return true;
        }

        /// <summary>
        ///     从好友缓存集合中移除缓存
        /// </summary>
        /// <param name="itemCache">要移除的缓存对象</param>
        /// <returns>返回false, 则表示移除失败</returns>
        public bool RemoveFriendCache(IMetaData friendCache)
        {
            //如果存在 --> 移除
            if (this.Equals(friendCache))
            {
                this.Collection.Remove(friendCache);
                return true;
            }
            return false;
        }

        /// <summary>
        ///     从好友缓存集合中移除具有指定Key对象
        /// </summary>
        /// <param name="Key">唯一表示</param>
        /// <returns>返回false, 则表示移除失败</returns>
        public bool RemoveFriendCache(String Key)
        {
            for (int i = 0; i < this.Collection.Count; i++)
            {
                if (this.Collection[i].Key == Key)
                {
                    this.Collection.Remove(this.Collection[i]);
                    return true;
                }
            }
            return false;
        }
    }
}
